package com.zyd.blog.personal.service.impl;
import java.util.ArrayList;
import	java.util.HashMap;

import com.zyd.blog.personal.enuminterface.IsDelete;
import com.zyd.blog.personal.enuminterface.Status;
import com.zyd.blog.personal.service.DesignerService;
import com.zyd.blog.personal.service.UserDetailInfoService;
import com.zyd.blog.personal.service.UserNoService;
import com.zyd.blog.personal.utils.DateUtils;
import com.zyd.blog.personal.utils.JavaBeanAndMapUtils;
import com.zyd.blog.personal.utils.StringUtils;
import com.zyd.blog.personal.vo.DesignerConditionVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 设计师业务逻辑层
 * @author mfy
 */
@Service
public class DesignerServiceImpl implements DesignerService {

    @Resource
    private UserDetailInfoService userDetailInfoService;

    @Resource
    private UserNoService userNoService;

    private List<Map<String,Object>> getDesignerList(Map<String,Object> designerConditionVo){
        Map<String,Object> condition = new HashMap<>(16);
        condition.put("isDelete", IsDelete.IS_DELETE_NO);
        if(designerConditionVo.size() != 0){
            condition.putAll(designerConditionVo);
        }
        return userDetailInfoService.getDesignerDetailInfo(condition);
    }

    /**
     * 格式化设计师信息
     * @param designerInfos 设计师信息
     * @return  格式化后的信息
     */
    private List<Map<String,Object>> formatDesignerInfo(List<Map<String,Object>> designerInfos){
        if(designerInfos == null || designerInfos.size() == 0){
            return new ArrayList<> ();
        }
        List<Map<String,Object>> result = new ArrayList<>();
        for (Map<String,Object> item : designerInfos){
            item.put("workType",StringUtils.parsingJsonArray((String) item.get("workType"),"content"));
            String status = (String) item.get("status");
            if("1".equals(status)){
                item.put("statusMsg","启用");
            }else{
                item.put("statusMsg","禁用");
            }
            item.put("applyTime", DateUtils.parseLongToString(Long.parseLong((String) item.get("applyTime")),"yyyy-MM-dd HH:mm:ss"));
            item.put("reviewTime", DateUtils.parseLongToString((Long) item.get("reviewTime"),"yyyy-MM-dd HH:mm:ss"));
            item.put("designTime", DateUtils.parseLongToString((Long) item.get("designTime"),"yyyy"));
            item.put("birthday", DateUtils.parseLongToString((Long) item.get("birthday"),"yyyy-MM-dd"));
            item.put("liveAddress", StringUtils.getAddressOnlyProvinceAndCity((String) item.get("liveAddress")));
            item.put("workAddress", StringUtils.getAddressOfStr((String) item.get("workAddress")));
            item.put("businessLicense", StringUtils.getStringOfSplit((String) item.get("businessLicense")));
            item.put("serviceAddress", StringUtils.getStringOfSplit((String) item.get("serviceAddress")));
            item.remove("userType");
            item.remove("homeAddress");
            item.remove("duty");
            item.remove("companyAddress");
            item.remove("joinTime");
            result.add(item);
        }
        return result;
    }

    @Override
    public List<Map<String,Object>> designerList(DesignerConditionVo designerConditionVo){
        Map<String,Object> condition = JavaBeanAndMapUtils.javaBeanToMap(designerConditionVo);
        List<Map<String,Object>> userInfos = getDesignerList(condition);
        return formatDesignerInfo(userInfos);
    }

    @Override
    public Map<String,Object> designerDetailPage(Long userId){
        if(userId == null){
            return new HashMap<>(0);
        }
        DesignerConditionVo designerConditionVo = new DesignerConditionVo();
        designerConditionVo.setUserId(userId);
        List<Map<String,Object>> designerDetailInfos = designerList(designerConditionVo);
        if(designerDetailInfos == null || designerDetailInfos.size() == 0){
            return new HashMap<> (0);
        }
        return designerDetailInfos.get(0);
    }

    @Override
    public boolean changeStatus(Long userId,Integer status){
        if(userId == null){
            return false;
        }
        if(status == null){
            return false;
        }
        Map<String,Object> condition = new HashMap<> (16);
        condition.put("id",userId);
        switch (status){
            case 0:
                //  禁用
                condition.put("status", Status.STATUS_NO);
                break;
            case 1:
                //  启用
                condition.put("status", Status.STATUS_YES);
                break;
            default : return false;
        }
        return userNoService.updateUserNo(condition);
    }

    @Override
    public boolean deleteDesigner(Long userId){
        if(userId == null){
            return false;
        }
        Map<String,Object> condition = new HashMap<> (16);
        condition.put("id",userId);
        condition.put("isDelete",IsDelete.IS_DELETE_YES);
        return userNoService.updateUserNo(condition);
    }

}
